package com.lanou.utp.service.impl;

import com.lanou.utp.advice.exception.MyException;
import com.lanou.utp.entity.*;
import com.lanou.utp.mapper.ClazzMapper;
import com.lanou.utp.mapper.CollegeMapper;
import com.lanou.utp.service.CollegeService;
import com.lanou.utp.utils.IdAndName;
import com.lanou.utp.utils.Level;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author MaQingYun
 */
@Transactional
@Service
public class CollegeServiceImpl implements CollegeService {

    @Resource
    private CollegeMapper collegeMapper;

    @Resource
    private ClazzMapper clazzMapper;

    //    @Cacheable(value = "school",keyGenerator = "MyKeyGenerator")
    @Override
    public School getOrgs() {
        return this.collegeMapper.getOrgs();
    }

    //    @Cacheable(value = "college",keyGenerator = "MyKeyGenerator")
    @Override
    public List<Major> getMajors(Long collegeId) {
        return this.collegeMapper.getMajors(collegeId);
    }

    @Override
    public List<IdAndName> getLeader(Integer level, Long id) {
        if (level == 3) {
            return this.collegeMapper.getLeaderByClazzId(id);
        } else if (level == 2) {
            return this.collegeMapper.getLeaderByMajorId(id);
        } else if (level == 1) {
            return this.collegeMapper.getLeader();
        }
        throw new MyException();
    }

    @Override
    public void addCollege(OrgCommonProperty parameterd) {
        if (parameterd.getLevel() == 0) {
            this.collegeMapper.addCollege(parameterd.getLeaderId(), parameterd.getName(), parameterd.getParentId());
        } else if (parameterd.getLevel() == 1) {
            this.collegeMapper.addMajor(parameterd.getLeaderId(), parameterd.getName(), parameterd.getParentId());
        } else if (parameterd.getLevel() == 2) {
            this.clazzMapper.insertClazz(parameterd);
        }

    }

    @Override
    public void updateOrganization(OrgCommonProperty parameter) {
        if (Level.FOR_COLLEGE == parameter.getLevel()) {
            this.collegeMapper.updateCollege(parameter);
        } else if (Level.FOR_MAJOR == parameter.getLevel()) {
            this.collegeMapper.updateMajor(parameter);
        } else if (Level.FOR_CLAZZ == parameter.getLevel()) {
            this.clazzMapper.updateClass(parameter);
        }
    }

    @Override
    public void deleteOrganization(OrgCommonProperty parameter) {
        if (parameter.getLevel() == 1) {
            this.collegeMapper.deleteCollege(parameter);
        } else if (parameter.getLevel() == 2) {
            this.collegeMapper.deleteMajor(parameter);
        } else if (parameter.getLevel() == 3) {
            this.clazzMapper.deleteClass(parameter);
        }
    }

    @Override
    public List<College> getCollegesWithSchool() {
        return this.collegeMapper.getCollegesWithSchool();
    }


}
